package com.gofaraway.service.左神算法课.heap;

import java.util.Arrays;
import java.util.Comparator;
import lombok.AllArgsConstructor;
import lombok.ToString;

/**
 * 比较器
 *
 * @author ChenPeng
 * @since 2025/2/16 15:42
 */
public class Code01_Comparator {


    public static void main(String[] args) {
        Student student1 = new Student("A", 2, 20);
        Student student2 = new Student("B", 3, 21);
        Student student3 = new Student("C", 1, 22);

        Student[] students = {student1, student2, student3};

        //按id降序
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.id - o1.id;
            }
        });

        System.out.println(Arrays.toString(students));

        //按id升序
        Arrays.sort(students, (o1, o2) -> o2.id - o1.id);
    }


    @AllArgsConstructor
    @ToString
    static class Student {

        private String name;
        private Integer age;
        private Integer id;
    }

}


